package uit.mmt02.mailControl;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import uit.mmt02.share.OneMail;

import android.util.Log;

public class MdeamonControl {
	String tag = "mdaemon";
	OneMail om;

	private String username;
	private String password;
	private int n_newmail;
	static final String HOSTSEND = "192.168.1.2";// 25
	static final String HOSTGET = "192.168.1.2";// 110
	//static final int SPORT = 587;

	public MdeamonControl(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}
	public int getN_newmail() {
		return n_newmail;
	}



	public void setN_newmail(int n_newmail) {
		this.n_newmail = n_newmail;
	}
	
	public boolean check_connect() // kiem tra ket noi den server mail
	{
		Log.v(tag, "check_connect...");
		Properties props = System.getProperties();
		props.setProperty("mail.store.protocol", "imaps");
		try 
		{
			props.put("mail.debug", "true");
			Session session = Session.getDefaultInstance(props, null);
			Store store = session.getStore("imaps");
			store.connect(HOSTGET,username, password);
            
            //lay so mail chua doc
            Folder inbox = store.getFolder("Inbox");
			inbox.open(Folder.READ_ONLY);
			setN_newmail(inbox.getUnreadMessageCount());
			Log.v(tag, "mail " +username + " chua doc: " + String.valueOf(inbox.getUnreadMessageCount())
					+ "--ton mail: " +String.valueOf(inbox.getMessageCount()));
			inbox.close(true);
            return true;
			
		}catch (Exception e) 
		{
			// TODO: handle exception
			Log.v(tag, "ko the ket noi" +e.toString());
			return false;
			
		}
	}
	
	public Message[] getmail2()
	{
		Message [] message = null;// message luu toan bo mail nhan duoc
		Properties props = System.getProperties();
		props.setProperty("mail.store.protocol", "pop3");
		try 
		{
			props.put("mail.debug", "true");
			Session session = Session.getDefaultInstance(props, null);
			Store store = session.getStore("pop3");
			store.connect(HOSTGET,username, password);
			Folder inbox = store.getFolder("Inbox");
			inbox.open(Folder.READ_ONLY);
			message = inbox.getMessages();
			Log.v(tag, "lay mail xong");
			
		} catch (Exception e) 
		{
			// TODO: handle exception
			Log.v(tag,"Loi get mail " + e.toString());
		}
		return message;
	}

	public Message[] getmail() {
		Message[] message = null;

		Properties properties = new Properties();
		properties.setProperty("mail.debug", "true");
		properties.setProperty("mail.store.protocol", "pop3");
		properties.setProperty("mail.pop3.port", "110");
		
		Session session = Session.getDefaultInstance(properties, null);
		try {
			Store store = session.getStore("pop3");
			store.connect(HOSTGET, 110, username, password);
//			store.connect(HOSTGET, username, password);
			Folder folder = store.getFolder("INBOX");
			folder.open(Folder.READ_ONLY);
			message = folder.getMessages();

		
			Log.v(tag, "lay mail xong");

		} catch (Exception e) {
			// TODO: handle exception
			Log.i(tag, "Loi get Mdeamon mail");
		}
		return message;
//		Message [] message = null;// message luu toan bo mail nhan duoc
//		Properties props = System.getProperties();
//		props.setProperty("mail.store.protocol", "pop3");
//		try 
//		{
//			props.put("mail.debug", "true");
//			Session session = Session.getDefaultInstance(props, null);
//			Store store = session.getStore("pop3");
//			store.connect(HOSTGET,username, password);
//			Folder inbox = store.getFolder("Inbox");
//			inbox.open(Folder.READ_ONLY);
//			message = inbox.getMessages();
//			Log.v(tag, "lay mail xong");
//			
//		} catch (Exception e) 
//		{
//			// TODO: handle exception
//			Log.v(tag,"Loi get mail " + e.toString());
//		}
//		return message;
	}

	public void send(String subject, String messbody, String toAddress, String[] attachURL) {


		try{
			
			Authenticator authenticator=null;

			// Configure your JavaMail.
			Properties props = new Properties();
			props.setProperty("mail.debug", "true");
			props.setProperty("mail.transport.protocol", "smtps");
			props.setProperty("mail.smtp.host",HOSTSEND);
			props.setProperty("mail.smtp.port", "25");
			props.setProperty("mail.smtps.auth", "true");
			authenticator = new Authenticator() {

				@Override
				protected PasswordAuthentication getPasswordAuthentication() {
					// TODO Auto-generated method stub
					return new PasswordAuthentication(username,password);
				}
				
			};


			Session session = Session.getDefaultInstance(props, authenticator);
			
			MimeMessage message1 = new MimeMessage(session);

	        message1.setFrom(new InternetAddress(username));

	        message1.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
	        
	        message1.setSubject(subject);

	        MimeBodyPart messagebodypart = new MimeBodyPart();
			messagebodypart.setText(messbody);
			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(messagebodypart);
			
			for(int i=0;i<attachURL.length;i++)
				if(!attachURL[i].equalsIgnoreCase("")){
					messagebodypart = new MimeBodyPart();
					DataSource source = new FileDataSource(attachURL[i]);
					messagebodypart.setDataHandler(new DataHandler(source));
					messagebodypart.setFileName(attachURL[i]);
					multipart.addBodyPart(messagebodypart);
				}
			
			message1.setContent(multipart);

			Transport transport = session.getTransport("smtp");
			transport.connect(HOSTSEND,25, username, password);
			transport.sendMessage(message1, message1.getAllRecipients());
			transport.close();
			
			Log.i(tag, "send MDEAMON thanh cong...");
		} catch (Exception e) {
			// TODO: handle exception
			Log.i(tag, "Loi gui MDEAMON");
		}
	}

	
}
